import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;


/**
 * 
 * @author Jonathan
 *
 */
public class RAReadBaseTable implements RAExpression {
	
	private String correlation;
	private String table;
	private Map<String, TableData> catalog;

	public RAReadBaseTable(Map<String, TableData> catalog, String correlation, String table)
	{
		this.correlation = correlation;
		this.table = table;
		this.catalog = catalog;
	}

	@Override
	public RAOutput execute() throws IOException
	{
		String filename = table + ".tbl";
		return new RAOutput(filename, catalog.get(table).getTableLayout(correlation));
	}

	@Override
	public void pushdownReferencedColumnsList(ArrayList<Expression> selectList)
	{
		// no op
	}

	@Override
	public <T> T accept(RAExpressionVisitor<T> v, Object... args)
	{
		return v.baseTableCase(args);
	}

	@Override
	public RAStatistics estimateCost(Map<String, TableData> catalog, Map<String, TableData> tempCatalog)
	{
		long cost = 0;
		long card = catalog.get(table).getTupleCount();
		return new RAStatistics(card, cost);
	}

	@Override
	public void getTables(Map<String, String> tables)
	{
		tables.put(correlation, table);
	}

	@Override
	public ArrayList<RAExpression> getJoinTransformations()
	{
		return null;
	}

	@Override
	public boolean referencedByExpression(Expression e)
	{
		return e.referencesTable(correlation);
	}

	@Override
	public void addWhereDisjunction(Expression e)
	{
		throw new RuntimeException("ERROR: Cannot add a where disjunction to a read base table operation.");
	}

	@Override
	public String joinPlanToString()
	{
		return correlation;
	}

	@Override
	public void applySelectList(ArrayList<Expression> select)
	{
		// no op
	}

	@Override
	public void pushdownJoinPredicates(ArrayList<Expression> joinWhereClauses)
	{
		// no op
	}

}
